package oa.hleast.xswl.service.pms.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.xswl.mapper.pms.PmsSpuMapper;
import oa.hleast.xswl.pojo.domain.pms.PmsSpu;
import oa.hleast.xswl.pojo.vo.pms.ShowPmsSpuVo;
import oa.hleast.xswl.service.ISysUserService;
import oa.hleast.xswl.service.pms.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 00302
 * @Date 2021/10/17 22:01:45
 */
@Slf4j
@Transactional
@Service
@AllArgsConstructor
public class AppPmsSpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements IAppPmsSpuService {

    private ISysUserService sysUserService;
    private IPmsSkuService iPmsSkuService;
    private IPmsSpuAttrValueService iPmsSpuAttrValueService;
    private IPmsSpuSpecValueService iPmsSpuSpecValueService;
    private IPmsCategorySpecService iPmsCategorySpecService;
    private final SpuReviewLogService spuReviewLogService;


    @Override
    public IPage<ShowPmsSpuVo> baseList(Integer page, Integer limit, Long categoryId) {
        log.info("page = {},limit = {} ,categoryId = {}", page, limit, categoryId);

        PmsSpu pmsSpu = new PmsSpu();
        pmsSpu.setCategoryId(categoryId);
        pmsSpu.setStatus(0);

        Page<PmsSpu> pojoPage = this.page(new Page(page, limit), new LambdaQueryWrapper<PmsSpu>()
                .eq(ObjectUtils.isNotEmpty(categoryId), PmsSpu::getCategoryId, categoryId)
                .eq(PmsSpu::getStatus, true)
        );

        List<ShowPmsSpuVo> voList = pojoPage.getRecords().stream().map(pojo -> {
            ShowPmsSpuVo showPmsSpuVo = new ShowPmsSpuVo();
            BeanUtil.copyProperties(pojo, showPmsSpuVo);
            return showPmsSpuVo;
        }).collect(Collectors.toList());


//        List<PmsSpu> list = this.baseMapper.listByStatus(pageObject, pmsSpu);
        Page<ShowPmsSpuVo> pageObject = new Page<>(page, limit, voList.size());
        pageObject.setRecords(voList);
        return pageObject;
    }
}
